package com.miaoshaproject.controller;

import com.miaoshaproject.error.BussinessException;
import com.miaoshaproject.error.EmBussinessError;
import com.miaoshaproject.response.CommonReturnType;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by kenny on 2019/1/5.
 */
public class BaseController {

    public  static final  String CONTENT_TYPE_FROMED="application/x-www-form-urlencoded";

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Object handlerException(HttpServletRequest request, Exception ex){
        Map<String,Object> responseData = new HashMap();
        if(ex instanceof BussinessException){
            BussinessException exception= (BussinessException) ex;
            responseData.put("errCode", exception.geteErrCode());
            responseData.put("errMsg",exception.getErrMsg());
        }else{
            responseData.put("errCode", EmBussinessError.UNKNOWN_ERROR.geteErrCode());
            responseData.put("errMsg",EmBussinessError.UNKNOWN_ERROR.getErrMsg());
        }



        return CommonReturnType.create(responseData,"fail");
    }
}
